home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Examples / Shark Attack / Sources & Headers / Stats.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  7.0 KB  |  242 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Stats.c    - routines for displaying the stats portion of the screen
  3. //
  4. // Created 9/20/97
  5. ///--------------------------------------------------------------------------------------
  6. #ifndef __RESOURCES__
  7. #include <Resources.h>
  8. #endif
  9.  
  10. #include <SWIncludes.h>
  11. #include <SWGameUtils.h>
  12. #include <SWApplication.h>
  13.  
  14. #include "Application.h"
  15. #include "Stats.h"
  16. #include "Shark Attack.h"
  17. #include "GlobalVariables.h"
  18.  
  19.  
  20. StatsRec    gLivesRec, gLevelRec, gScoreRec;
  21.  
  22.  
  23. ///--------------------------------------------------------------------------------------
  24. //  InitStats - create offscreen and window frames for stats area. This function is
  25. //    called only once when the program first start up.
  26. ///--------------------------------------------------------------------------------------
  27.  
  28. void    InitStats( void )
  29. {
  30.     PixPatHandle    statsPixPatH;
  31.     Rect            statsRect;
  32.     OSErr            err;
  33.     
  34.     statsRect = gSpriteWorldP->windRect;
  35.     statsRect.bottom = statsRect.top + kStatsHeight;
  36.     
  37.     err = SWCreateWindowFrame((CWindowPtr)gWindowP, &gStatsWindowFrameP, &statsRect, 0);
  38.     FatalError(err);
  39.     
  40.     OffsetRect(&statsRect, -statsRect.left, -statsRect.top);
  41.     err = SWCreateFrame(gSpriteWorldP->mainSWGDH, &gStatsBackFrameP, &statsRect, 
  42.             gSpriteWorldP->pixelDepth, true);
  43.     FatalError(err);
  44.     
  45.     SWLockWindowFrame(gStatsWindowFrameP);
  46.     SWLockFrame(gStatsBackFrameP);
  47.     
  48.     statsPixPatH = GetPixPat(131);
  49.     if (statsPixPatH == NULL)
  50.         CantFindResource();
  51.     
  52.         // Fill stats area with grey
  53.     statsRect = gStatsBackFrameP->frameRect;
  54.     SetGWorld(gStatsBackFrameP->framePort, nil);
  55.     FillCRect(&statsRect, statsPixPatH);
  56.     DisposePixPat(statsPixPatH);
  57.     
  58.         // Draw a black & white frame around the stats area
  59.     ForeColor(whiteColor);
  60.     FrameRect(&statsRect);
  61.     
  62.     InsetRect(&statsRect, 1, 1);
  63.     ForeColor(blackColor);
  64.     FrameRect(&statsRect);
  65.     
  66.         // Set certain fields of each StatsRec
  67.     gLivesRec.theFrameP = gStatsWindowFrameP;
  68.     gLivesRec.numDigits = 1;
  69.     gLivesRec.justification = kRightJustify;
  70.     
  71.     gLevelRec.theFrameP = gStatsWindowFrameP;
  72.     gLevelRec.numDigits = 2;
  73.     gLevelRec.justification = kRightJustify;
  74.  
  75.     gScoreRec.theFrameP = gStatsWindowFrameP;
  76.     gScoreRec.numDigits = 5;
  77.     gScoreRec.justification = kLeftJustify;
  78.     
  79.         // Load the picture for each area (lives, level, and score)
  80.     DrawPictInStatsArea(kLivesPictResID);
  81.     DrawPictInStatsArea(kLevelPictResID);
  82.     DrawPictInStatsArea(kScorePictResID);
  83.     
  84.         // Load the picture containing our digits into a Frame
  85.     err = SWCreateFrameFromPictResource(gSpriteWorldP, &gStatsNumberFrameP, 
  86.             kNumbersPictResID, 0, kNoMask);
  87.     FatalError(err);
  88.     
  89.     SWLockFrame(gStatsNumberFrameP);
  90. }
  91.  
  92.  
  93. ///--------------------------------------------------------------------------------------
  94. //  DrawPictInStatsArea - used by InitStats. Assumes correct port is already set.
  95. ///--------------------------------------------------------------------------------------
  96.  
  97. void    DrawPictInStatsArea( short pictID )
  98. {
  99.     PicHandle     myPictH;
  100.     Rect        pictRect;
  101.  
  102.     myPictH = GetPicture(pictID);
  103.     if (myPictH != NULL)
  104.     {
  105.         pictRect = (**myPictH).picFrame;
  106.         CenterRect(&pictRect, &gStatsBackFrameP->frameRect);
  107.         
  108.             // Determine where to draw pict, and save position for drawing numbers later
  109.         if (pictID == kLivesPictResID)
  110.         {
  111.             OffsetRect(&pictRect, -pictRect.left + 40, 0);
  112.             gLivesRec.horizLoc = pictRect.left + kLivesNumOffset;
  113.             gLivesRec.vertLoc = pictRect.top + kStatsVertOffset;
  114.         }
  115.         else if (pictID == kLevelPictResID)
  116.         {
  117.             OffsetRect(&pictRect, -20, 0);
  118.             gLevelRec.horizLoc = pictRect.left + kLevelNumOffset;
  119.             gLevelRec.vertLoc = pictRect.top + kStatsVertOffset;
  120.         }
  121.         else    // kScorePictResID
  122.         {
  123.             OffsetRect(&pictRect, gStatsBackFrameP->frameRect.right - 
  124.                     pictRect.right - 40, 0);
  125.             gScoreRec.horizLoc = pictRect.left + kScoreNumOffset;
  126.             gScoreRec.vertLoc = pictRect.top + kStatsVertOffset;
  127.         }
  128.         
  129.         DrawPicture(myPictH, &pictRect);
  130.         ReleaseResource((Handle)myPictH);
  131.     }
  132.     else
  133.         CantFindResource();
  134. }
  135.  
  136.  
  137. ///--------------------------------------------------------------------------------------
  138. //  UpdateStatsArea - copies the contents of the gStatsBackFrameP to the gStatsWindowFrameP
  139. ///--------------------------------------------------------------------------------------
  140.  
  141. void    UpdateStatsArea( void )
  142. {
  143.     SetGWorld(gStatsWindowFrameP->framePort, nil);
  144.         
  145.     (*gSpriteWorldP->screenDrawProc)(gStatsBackFrameP, gStatsWindowFrameP,
  146.             &gStatsBackFrameP->frameRect, &gStatsWindowFrameP->frameRect);
  147.     
  148.     ResetStats();    // Force the numbers to redraw, by making them think they've changed
  149.     UpdateStatsNumbers();    // Then draw them
  150. }
  151.  
  152.  
  153. ///--------------------------------------------------------------------------------------
  154. //  UpdateStatsNumbers - redraws the numbers for each stats area if they've changed
  155. ///--------------------------------------------------------------------------------------
  156.  
  157. void    UpdateStatsNumbers( void )
  158. {
  159.         // Stuff lives, level, and score data into each StatsRec
  160.     gLivesRec.theNum = gNumLivesLeft;
  161.     gLevelRec.theNum = gCurrentLevel;
  162.     gScoreRec.theNum = gScore;
  163.     
  164.     UpdateStatsRec(&gLivesRec);
  165.     UpdateStatsRec(&gLevelRec);
  166.     UpdateStatsRec(&gScoreRec);
  167. }
  168.  
  169.  
  170. ///--------------------------------------------------------------------------------------
  171. //  ResetStats - called before a new game to make sure the new stats get drawn
  172. ///--------------------------------------------------------------------------------------
  173.  
  174. void    ResetStats( void )
  175. {
  176.     gLivesRec.oldNum = -1;
  177.     gLevelRec.oldNum = -1;
  178.     gScoreRec.oldNum = -1;
  179. }
  180.  
  181.  
  182. ///--------------------------------------------------------------------------------------
  183. //  UpdateStatsRec - redraws the number for a particular statsRec if it has changed
  184. ///--------------------------------------------------------------------------------------
  185.  
  186. void    UpdateStatsRec( StatsRecPtr statsRecP )
  187. {
  188.     short    theRow, theCol, theNum, theDigit;
  189.     short    length = 0;
  190.     Rect    srcRect, dstRect;
  191.     
  192.         // Only draw the number if it has changed since last time
  193.     if (statsRecP->oldNum != statsRecP->theNum)
  194.     {
  195.         statsRecP->oldNum = statsRecP->theNum;
  196.         
  197.         theRow = statsRecP->theFrameP->frameRect.top + statsRecP->vertLoc;
  198.         theCol = statsRecP->theFrameP->frameRect.left + statsRecP->horizLoc;
  199.         theNum = statsRecP->theNum;
  200.         if (theNum < 0)
  201.             theNum = 0;
  202.         
  203.         do    // calculate length of the number
  204.         {
  205.             length++;
  206.             theNum /= 10;
  207.         } while (theNum > 0);
  208.         
  209.         if (statsRecP->justification == kLeftJustify)
  210.             theCol += (length-1) * kNumberWidth;
  211.         else
  212.             theCol += (statsRecP->numDigits-1) * kNumberWidth;
  213.         
  214.         theNum = statsRecP->theNum;
  215.         if (theNum < 0)
  216.             theNum = 0;
  217.         
  218.         do
  219.         {
  220.             theDigit = theNum%10;
  221.             
  222.             srcRect.top = 0;
  223.             srcRect.bottom = kNumberHeight;
  224.             srcRect.left = theDigit * kNumberWidth;
  225.             srcRect.right = srcRect.left + kNumberWidth;
  226.             
  227.             dstRect.left = theCol;
  228.             dstRect.top = theRow;
  229.             dstRect.right = theCol + kNumberWidth;
  230.             dstRect.bottom = theRow + kNumberHeight;
  231.             
  232.                 // Copy the digit using the SpriteWorld's current screenDrawProc
  233.             (*gSpriteWorldP->screenDrawProc)(gStatsNumberFrameP, gStatsWindowFrameP,
  234.                     &srcRect, &dstRect);
  235.             
  236.             theCol -= kNumberWidth;
  237.             theNum /= 10;
  238.         } while (theNum > 0);
  239.     }
  240. }
  241.  
  242.